const app = getApp();
// socket连接
const webSocket = require('../../utils/websock');
const Breath = require('../../utils/breath');
const DateFormat = require('../../utils/util');
Page({
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    //深色
    bottomColor: '#00c2cf',
    //浅色
    gradientColor:'#00d2a9',
    heartNum:80,
    breathRate:0.5,
    canvasWidth:0,
    query:null
  },

  onLoad: function (options) {
    let that = this;
    // 页面加载进来
    wx.getSystemInfo( {
        success: function( res ) {
            that.setData( {
                canvasWidth: res.windowWidth
            });
        }
    });

    //获取当前日期,打鼾、体动
    let riqi = new Date();
    this.setData({
      dateTime:DateFormat.formatTime(riqi),
      snoringAvg:app.globalData.snoringAvg,
      bodyAvg:app.globalData.bodyAvg
    });
    
    // 呼吸曲线动效
    this.query = wx.createSelectorQuery().in(this);
    this.query.select('#heartline')
      .fields({ node: true, size: true })
      .exec((res) => {
        Breath.start_herat(res);
    })

     console.log("当前设备编号======",app.globalData.deviceCode)
    // 创建连接
     webSocket.openSocket();

    // 设置接收消息回调
     webSocket.onSocketMessageCallback = this.onSocketMessageCallback;
  },

  onSocketMessageCallback:function(res){
    // console.log('=======================回调', res)
    this.setData({
      heartNum:res.heartRate,
      breathRate:res.breathRate
    })
  },

  // 页面销毁时关闭连接
  onUnload: function () {
    // webSocket.closeSocket();
  },


  onReady: function () {
    
  },


  onShow: function () {
    
  },


  onHide: function () {
    
  },



  onPullDownRefresh: function () {
    
  },

  onReachBottom: function () {
    
  },


  onShareAppMessage: function () {
    
  }
})